package com.activiti;

import java.util.ArrayList;
import java.util.List;

import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhangbowen@heytea.com
 * @since 2020-05-22 11:04
 */
@RestController
public class MyRestController {

    @Autowired
    private TestService testService;

    @RequestMapping(value = "/process", method = RequestMethod.POST)
    public String startProcessInstance() {
        return testService.startProcess();
    }

    @RequestMapping(value = "/tasks", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public List<TaskRepresentation> getTasks(@RequestParam String processId, @RequestParam String assignee) {

        List<Task> tasks = testService.getTasks(processId, assignee);
        List<TaskRepresentation> dtos = new ArrayList<>();
        for (Task task : tasks) {
            dtos.add(new TaskRepresentation(task.getId(), task.getName()));
        }
        return dtos;
    }

    @RequestMapping(value = "/planProject", method = RequestMethod.GET)
    public void commitPlanProject(@RequestParam String taskId, @RequestParam Integer money) {
        testService.commitTaskPlanProject(taskId, money);
    }

    @RequestMapping(value = "/signStart", method = RequestMethod.GET)
    public void commitSignStart(@RequestParam String taskId) {
        testService.commitSignStart(taskId);
    }

}
